<?php
    session_start();
    require '../db_connection.php';

    $type = isset($_GET['type']) ? $_GET['type'] : '';

    if ($type == 'delete') {
        $id = $_GET['id'];
        $sql = "DELETE FROM educational_resources WHERE id = $id";
        $result = $conn->query($sql);
    }
  
    $perpage = 10; 
    $start_index = ((isset($_GET['p']) ? $_GET['p'] : 1) - 1) * $perpage;

    $keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';
    if (!empty($keyword)) {
        $where = "WHERE resource_name LIKE '%" . $keyword . "%'";
    } else {
        $where = "WHERE 1 = 1";
    }
    $sql = "SELECT * FROM educational_resources $where ORDER BY id DESC LIMIT $start_index, $perpage";
    $mysqli_res = mysqli_query($conn, $sql);
    $list = [];
    while($row = $mysqli_res->fetch_assoc()) {
        $list[] = $row;
    }

    $sql = "SELECT COUNT(*) AS total FROM educational_resources $where";
    $mysqli_res = mysqli_query($conn, $sql);    
    $total = $mysqli_res->fetch_assoc()['total'];

    mysqli_close($conn);

    $total_pages = ceil($total / $perpage);

    $prev_page = (isset($_GET['p']) && $_GET['p'] > 1) ? $_GET['p']
        - 1 : 1;
    $next_page = (isset($_GET['p']) && $_GET['p'] < $total_pages) ? $_GET['p']  
        + 1 : $total_pages;
    $pagination = "<div class='pagelist'>";
    
    $pagination .= "<a href='resource_list.php?p=1&keyword=$keyword'>首页</a>";
    $pagination .= "<a href='resource_list.php?p=$prev_page&keyword=$keyword'>上一页</a>";
    for ($i = 1; $i <= $total_pages; $i++) {
        $pagination .= "<a href='resource_list.php?p=$i&keyword=$keyword'>$i</a>";
    }
    $pagination .= "<a href='resource_list.php?p=$next_page&keyword=$keyword'>下一页</a>";
    $pagination .= "<a href='resource_list.php?p=$total_pages&keyword=$keyword'>尾页</a>";
    $pagination .= "</div>";
?>
<?php require 'common/head.php'; ?>
</head>
<body>
<form method="post" action="" id="listform">
  <div class="panel admin-panel">
    <div class="panel-head"><strong class="icon-reorder">资源管理</strong></div>
    <div class="padding border-bottom">
      <ul class="search" style="padding-left:10px;">
        <li>
          <input type="text" placeholder="输入资源名称" value="<?php echo isset($_GET['keyword']) ? $_GET['keyword'] :''; ?>" name="keyword" class="input" style="width:250px; line-height:17px;display:inline-block" />
          <a href="javascript:void(0)" class="button border-main icon-search" onclick="changesearch()"> 搜索</a></li>
      </ul>
    </div>
    <table class="table table-hover text-center" style="color:#000;">
        <tr>
            <th width="100" style="text-align:left; padding-left:20px;">ID</th>
            <th>资源名称</th>
            <th>类型</th>
            <th>描述</th>
            <th>上传时间</th>
            <th>下载地址</th>
            <th>操作</th>
        </tr>
        <?php foreach($list as $row) { ?>
        <tr>
            <td style="text-align:left; padding-left:20px;"><?php echo $row['id']; ?></td>
            <td><?php echo $row['resource_name']; ?></td>
            <td><?php echo $row['type']; ?></td>
            <td><?php echo $row['description']; ?></td>
            <td><?php echo $row['upload_time']; ?></td>
            <td><a href="/<?php echo $row['file_path']; ?>"><?php echo $row['file_path']; ?></a></td>
            <td width="300">
                <div class="button-group">
                    <a class="button border-red" href="javascript:void(0)" onclick="window.location.href='resource_list.php?id=<?php echo $row['id']; ?>&type=delete'">删除</a>
                    <a class="button border-green" href="javascript:void(0)" onclick="window.location.href='resource_edit.php?id=<?php echo $row['id']; ?>'">编辑</a>
                </div>
            </td>
        </tr>
        <?php } ?>
        <?php if ($total_pages) { ?>
        <tr>
            <td colspan="8">
                <?php echo $pagination; ?>
            </td>
        </tr>
        <?php } ?>
    </table>
    </div>
</form>
</div>
<script>
    function changesearch() {	
        let keyword = $('input[name=keyword]').val()
        location.href = 'resource_list.php?keyword='+keyword
    }
</script>
</body>
</html>

